package calin.podcast;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

/**
 * Simple podcasts database access helper class. Defines the basic CRUD
 * operations for the calin podcast, and gives the ability to list all podcasts
 * as well as retrieve or modify a specific podcast.
 * 
 * This has been improved from the first version of this tutorial through the
 * addition of better error handling and also using returning a Cursor instead
 * of using a collection of inner classes (which is less scalable and not
 * recommended).
 */
public class CalinPodcastDbAdapter {

	public static final String KEY_TITLE = "title";
	public static final String KEY_URL = "url";
	public static final String KEY_ROWID = "_id";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;

	private static final String DATABASE_TABLE = "podcasts";
	
	private final Context mCtx;

	private static class DatabaseHelper extends CalinDatabaseHelper {

		DatabaseHelper(Context context) {
			super(context);
		}
	}

	/**
	 * Constructor - takes the context to allow the database to be
	 * opened/created
	 * 
	 * @param ctx
	 *            the Context within which to work
	 */
	public CalinPodcastDbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	/**
	 * Open the podcasts database. If it cannot be opened, try to create a new
	 * instance of the database. If it cannot be created, throw an exception to
	 * signal the failure
	 * 
	 * @return this (self reference, allowing this to be chained in an
	 *         initialization call)
	 * @throws SQLException
	 *             if the database could be neither opened or created
	 */
	public CalinPodcastDbAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	/**
	 * Create a new podcast using the title and url provided. If the podcast is
	 * successfully created return the new rowId for that podcast, otherwise
	 * return a -1 to indicate failure.
	 * 
	 * @param title
	 *            the title of the podcast
	 * @param url
	 *            the url of the podcast
	 * @return rowId or -1 if failed
	 */
	public long createPodcast(String title, String url) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_TITLE, title);
		initialValues.put(KEY_URL, url);

		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}

	/**
	 * Delete the podcast with the given rowId
	 * 
	 * @param rowId
	 *            id of podcast to delete
	 * @return true if deleted, false otherwise
	 */
	public boolean deletePodcast(long rowId) {

		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	/**
	 * Return a Cursor over the list of all podcasts in the database
	 * 
	 * @return Cursor over all podcasts
	 */
	public Cursor fetchAllPodcasts() {

		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TITLE,
				KEY_URL }, null, null, null, null, null);
	}

	/**
	 * Return a Cursor positioned at the podcast that matches the given rowId
	 * 
	 * @param rowId
	 *            id of podcast to retrieve
	 * @return Cursor positioned to matching podcast, if found
	 * @throws SQLException
	 *             if podcast could not be found/retrieved
	 */
	public Cursor fetchPodcast(long rowId) throws SQLException {

		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TITLE,
				KEY_URL }, KEY_ROWID + "=" + rowId, null, null, null, null,
				null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	/**
	 * Update the podcast using the details provided. The podcast to be updated
	 * is specified using the rowId, and it is altered to use the title and url
	 * values passed in
	 * 
	 * @param rowId
	 *            id of podcast to update
	 * @param title
	 *            value to set podcast title to
	 * @param url
	 *            value to set podcast url to
	 * @return true if the podcast was successfully updated, false otherwise
	 */
	public boolean updatePodcast(long rowId, String title, String url) {
		ContentValues args = new ContentValues();
		args.put(KEY_TITLE, title);
		args.put(KEY_URL, url);

		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}
}
